package com.shujia.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class PhoenixDemo {
    public static void main(String[] args) throws Exception {
        //注册驱动
        Class.forName("org.apache.phoenix.jdbc.PhoenixDriver");

        //创建链接对象
        Connection conn = DriverManager.getConnection("jdbc:phoenix:master,node1,node2:2181");

        //select /*+ INDEX(DIANXIN DIANXIN_INDEX) */  * from DIANXIN where end_date = '20180503154014'  and start_date = '20180503154614';
        //创建预编译对象，防止sql注入
        PreparedStatement prep = conn.prepareStatement("select /*+ INDEX(DIANXIN DIANXIN_INDEX) */  * from DIANXIN where end_date = ?  and start_date = ?");

        //设置参数值
        prep.setString(1, "20180503154014");
        prep.setString(2, "20180503154614");

        //执行sql语句
        ResultSet resultSet = prep.executeQuery();

        //遍历得到每一列
        while (resultSet.next()) {
            String mdn = resultSet.getString("mdn");
            String county = resultSet.getString("county");

            System.out.println("手机号：" + mdn + ", 城市编号：" + county);
        }

        //释放资源
        prep.close();
        conn.close();

    }
}
